<?php

namespace app\api\controller;

use app\admin\model\Feature;
use app\common\controller\Api;
use Carbon\Carbon;

/**
 * 首页接口
 */
class Store extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function all()
    {
        $stores = \app\common\model\Store::where('valid_time', '>', time())->select();
        $stores = array_map(function ($e) {
            $e->store_bannerimage = url($e->store_bannerimage, '', false, true);
            $e->store_slideimages = (function ($value) {
                $rows = explode(',', $value);
                return array_map(function ($e) {
                    return url($e, '', false, true);
                }, $rows);
            })($e->store_slideimages);
            return $e;
        }, $stores);

        $this->success('OK', $stores);
    }

    public function baseinfo()
    {
        $id = $this->request->get('id', null);
        if (empty($id)) {
            $store = \app\common\model\Store::order('id', 'asc')->find();
            if (is_null($store)) {
                $this->error('没有门店数据');
            }
        } else {
            $store = \app\common\model\Store::get($id);
        }

        $store->store_bannerimage = url($store->store_bannerimage, '', false, true);
        $store->store_slideimages = (function ($value) {
            $rows = explode(',', $value);
            return array_map(function ($e) {
                return url($e, '', false, true);
            }, $rows);
        })($store->store_slideimages);

        $storeReservationCount = \app\admin\model\Reserve::where('store_id', $id)->where('starttime', '>', Carbon::now()->subMonth(3)->toDateTimeString())->count();
        $storeReservationCount = $storeReservationCount > 50 ? $storeReservationCount : '50+';

        $store->promotion_rent_info = "近期有{$storeReservationCount}次预定";
        $store->features = (function ($ids) {
            $features = Feature::whereIn('id', $ids)->select();
            return array_map(function ($e) {
                return [
                    'id' => $e->id,
                    'title' => $e->feature_name,
                    'color' => $e->color,
                    'icon_url' => url($e->iconimage, '', false, true),
                ];
            }, $features);
        })($store->feature_ids);

        $rooms = $store->rooms()->order('id', 'desc')->select();
        $rooms = array_map(function ($e) {
            $e->room_bannerimage = url($e->room_bannerimage, '', false, true);
            $roomId = $e->id;
            $lastReservation = \app\admin\model\Reserve::where('room_id', $roomId)->order('id', 'desc')->find();
            if ($lastReservation) {
                $diffHours = Carbon::now()->diffInHours(Carbon::createFromTimestamp($lastReservation->createtime));
                if ($diffHours > 8) {
                    $e->promotion_rent_pre_hour = 8;
                } else {
                    $e->promotion_rent_pre_hour = $diffHours < 1 ? 1 : $diffHours;
                }
            } else {
                $e->promotion_rent_pre_hour = 8;
            }
            $e->features = (function ($ids) {
                $features = Feature::whereIn('id', $ids)->select();
                return array_map(function ($e) {
                    return [
                        'id' => $e->id,
                        'title' => $e->feature_name,
                        'color' => $e->color,
                        'icon_url' => url($e->iconimage, '', false, true),
                    ];
                }, $features);
            })($e->feature_ids);
            return $e;
        }, $rooms);
        // 过滤状态
        $newRooms = [];
        foreach ($rooms as $r) {
            if ($r->status == 'normal') {
                $newRooms[] = $r;
            }
        }

        $this->success('OK', ['store' => $store, 'rooms' => $newRooms]);
    }
}
